﻿using Heng.Elements.Core.IOC;
using Heng.Elements.Core.Model.Infrastructure.Entities;
using Heng.Elements.Core.Model.Infrastructure.Repositories;
using Heng.Elements.Core.Model.Infrastructure.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Heng.Elements.Core.Model.Infrastructure.IOC
{
    /// <summary>
    /// Model extension methods for IOC.
    /// </summary>
    public static class IocExtensions
    {
        /// <summary>
        /// Get service of specific type.
        /// </summary>
        /// <typeparam name="TService"> Service type. </typeparam>
        /// <param name="ioc"> IOC instance. </param>
        /// <returns> Service instance. </returns>
        public static TService GetServiceByType<TService>(this Ioc ioc)
            where TService : IModelService
        {
            return ioc.GetInstance<TService>();
        }

        /// <summary>
        /// Get entity service.
        /// </summary>
        /// <typeparam name="TEntity"> Entity type. </typeparam>
        /// <param name="ioc"> IOC instance. </param>
        /// <returns> Entity service instance. </returns>
        public static IEntityService<TEntity> GetEntityServiceByEntityType<TEntity>(this Ioc ioc)
            where TEntity : class, IEntity
        {
            return ioc.GetInstance<IEntityService<TEntity>>();
        }

        /// <summary>
        /// Get entity repository.
        /// </summary>
        /// <typeparam name="TEntityRepository"> Repository type. </typeparam>
        /// <param name="ioc"> IOC instance. </param>
        /// <returns> Entity repository instance. </returns>
        public static TEntityRepository GetEntityRepositoryByType<TEntityRepository>(this Ioc ioc)
            where TEntityRepository : IEntityRepository
        {
            return ioc.GetInstance<TEntityRepository>();
        }

        /// <summary>
        /// Get entity repository.
        /// </summary>
        /// <typeparam name="TEntity"> Entity type. </typeparam>
        /// <param name="ioc"> IOC instance. </param>
        /// <returns> Entity repository instance. </returns>
        public static IEntityRepository<TEntity> GetEntityRepositoryByEntityType<TEntity>(this Ioc ioc)
            where TEntity : class, IEntity
        {
            return ioc.GetInstance<IEntityRepository<TEntity>>();
        }
    }
}
